package com.zgw.his.design.editor;

import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.zgw.his.common.DateUtil;
import com.zgw.his.design.model.Template;
import com.zgw.his.design.resource.MedicalResourceManager;
import com.zgw.his.server.emr.entity.Temrtemplate;

/**
 * 模版信息面版
 * */
public class TemplateInfoComposite extends Composite {
	private Text textname;
	private Text textnumber;
	private Text textheight;
	private Text textwidth;
	private Text texttime;
	private Temrtemplate temrtemplate;
	private Combo combostandard;
	private MedicalEditor m_editor;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 * @param editor
	 */
	public TemplateInfoComposite(Composite parent, int style,
			MedicalEditor editor) {
		super(parent, style);
		this.m_editor = editor;
		createControls();
	}

	private void createControls() {
		setLayout(new FormLayout());
		{
			Group group = new Group(this, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.top = new FormAttachment(0, 10);
				formData.left = new FormAttachment(0, 10);
				formData.right = new FormAttachment(100, -10);
				group.setLayoutData(formData);
			}
			group.setText("基本信息");
			group.setLayout(new GridLayout(3, false));
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("名称：");
			}
			{
				textname = new Text(group, SWT.BORDER);
				textname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						false, false, 1, 1));
			}
			new Label(group, SWT.NONE);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("编号：");
			}
			{
				textnumber = new Text(group, SWT.BORDER);
				textnumber.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						false, false, 1, 1));
			}
			new Label(group, SWT.NONE);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("创建时间：");
			}
			{
				texttime = new Text(group, SWT.BORDER);
				texttime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						false, false, 1, 1));
			}
			new Label(group, SWT.NONE);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("规格：");
			}
			{
				combostandard = new Combo(group, SWT.READ_ONLY);
				{
					GridData gridData = new GridData(SWT.LEFT, SWT.CENTER,
							false, false, 1, 1);
					gridData.widthHint = 85;
					combostandard.setItems(new String[] { "A4", "A5", "A6" });
					combostandard.setLayoutData(gridData);
				}
			}
			new Label(group, SWT.NONE);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("页长：");
			}
			{
				textheight = new Text(group, SWT.BORDER);
				textheight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						false, false, 1, 1));
			}
			new Label(group, SWT.NONE);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setText("页宽：");
			}

			{
				textwidth = new Text(group, SWT.BORDER);
				textwidth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						false, false, 1, 1));
			}
			{
				Button btns = new Button(group, SWT.NONE);
				btns.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true,
						false, 1, 1));
				btns.setText("确认修改");
				btns.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						doEditBasicInfo();
					}
				});
			}
		}
		initializeListener();
	}

	private void initializeListener() {
		combostandard.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelectedStandard();
			}
		});
	}

	protected void doSelectedStandard() {
		if (combostandard.getText().equals("A4")) {
			textheight.setText("1200");
			textwidth.setText("793");
		} else if (combostandard.getText().equals("A5")) {
			textheight.setText("792");
			textwidth.setText("559");
		} else if (combostandard.getText().equals("A6")) {
			textheight.setText("560");
			textwidth.setText("400");
		}
	}

	// 确认修改基础信息
	protected void doEditBasicInfo() {
		temrtemplate = (Temrtemplate) m_editor.getMedicalTemplate().getData();
		Template template = m_editor.getMedicalTemplate();
		if (temrtemplate != null) {
			temrtemplate.setFname(textname.getText());
			temrtemplate.setFnumber(textnumber.getText());
			try {
				m_editor.getResourceManager().modifyInfo(template, false);
				m_editor.refreshUI();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog.openError(getShell(), "修改失败", e.getMessage());
			}
		}
	}

	public void setInput(Object obj) {
		if (obj instanceof Temrtemplate) {
			this.temrtemplate = (Temrtemplate) obj;
			this.textname.setText(toString(temrtemplate.getFname()));
			this.textnumber.setText(toString(temrtemplate.getFnumber()));
			this.texttime.setText(toString(temrtemplate.getFcreatetime()));
		}
	}

	public String getHeight() {
		return textheight.getText();
	}

	public void setHeight(String height) {
		this.textheight.setText(height);
		if (height.trim().equals("1200")) {
			combostandard.setText("A4");
		} else if (height.trim().equals("792")) {
			combostandard.setText("A5");
		} else if (height.trim().equals("560")) {
			combostandard.setText("A6");
		}
	}

	public String getWidth() {
		return textwidth.getText();
	}

	public void setWidth(String width) {
		this.textwidth.setText(width);
	}

	public Temrtemplate getTemrtemplate() {
		return temrtemplate;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	private String toString(Object obj) {
		if (obj instanceof String) {
			return (String) obj;
		} else if (obj instanceof Date) {
			DateUtil.toString((Date) obj);
		}
		return "";
	}

}
